package library.and.xps.com.step.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.jude.utils.JUtils;
import com.orhanobut.logger.Logger;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import library.and.xps.com.step.R;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.TeamChallengeRes;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.view.activity.TeamChallengeDetailActivity;

//团队挑战
public class TeamChallengeAdapter extends AbstractRecyclerAdapter<TeamChallengeRes.ListBean> {

    public TeamChallengeAdapter(Context context, RecyclerView recyclerView) {
        super(context, recyclerView);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.team_challenge_item, parent, false);
        return new TeamChallengeViewHolder(v);
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, int position, boolean isItem) {
        TeamChallengeViewHolder myViewHolder = (TeamChallengeViewHolder) holder;
        final TeamChallengeRes.ListBean data = getItem(position);
        Logger.d(data.toString());

        CommonUtils.setupCircleImgWithToken(context, myViewHolder.iv_bechallenger_header, data.getBico());
        CommonUtils.setupCircleImgWithToken(context, myViewHolder.iv_challenger_header, data.getCico());

        myViewHolder.tv_read_time.setText(data.getReadingtimes());
        myViewHolder.tv_bechallenger_exc.setText(data.getBsteps() + "");
        myViewHolder.tv_challenger_exc.setText(data.getSteps() + "");
        myViewHolder.tv_challenger_name.setText(data.getCname());
        myViewHolder.tv_bechallenger_name.setText(data.getBname());
        myViewHolder.al_item_challenger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.getState() == 0 || data.getState() == 1 || data.getState() == 2) {
                    Intent i = new Intent(context, TeamChallengeDetailActivity.class);
                    i.putExtra("pkid", data.getUserpkid());
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
        });

        initChallengerState(myViewHolder,data.getState());
    }

    private void initChallengerState(TeamChallengeViewHolder myViewHolder, int state) {
        switch (state) {
            case -1://已取消
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#f4f4f4"));
                myViewHolder.tv_state.setText("已取消");
                myViewHolder.tv_state.setTextColor(Color.parseColor("#7e7e7e"));
                break;
            case 0://等待响应
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#FF489DFB"));
                myViewHolder.tv_state.setText("等待响应");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 1://即将开始
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#FF489DFB"));
                myViewHolder.tv_state.setText("即将开始");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 2://进行中
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#f67070"));
                myViewHolder.tv_state.setText("进行中");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 3://拒绝
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#FFD700"));
                myViewHolder.tv_state.setText("拒绝");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 4://结束
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#f4f4f4"));
                myViewHolder.tv_state.setText("结束");
                myViewHolder.tv_state.setTextColor(Color.parseColor("#7e7e7e"));
                break;
        }
    }


    public void setupCircleImg(Context context, ImageView imageView, String url) {
        if ("null".equals(url) || "NULL".equals(url) || TextUtils.isEmpty(url)) return;

        String imgUrl = URLConstants.FILE.FileDownload + url;

        GlideUrl glideUrl = new GlideUrl(imgUrl, new LazyHeaders.Builder()
                .addHeader("token", JUtils.getSharedPreference().getString(SPConstant.USER_TOKEN, ""))
                .build());

        Glide.with(context).load(glideUrl)
                .placeholder(R.drawable.default_header_male)
                .bitmapTransform(new CropCircleTransformation(context))
                .into(imageView);
    }
}
